package com.todod.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.todod.dto.RoleDto;
import com.todod.entity.ReqRoleFile;
import com.todod.entity.ResultData;
import com.todod.model.PublicFileShare;
import com.todod.model.Role;
import com.todod.model.User;
import com.todod.model.UserRole;
import com.todod.service.IRoleService;
import com.todod.service.IUserRoleService;
import com.todod.service.IUserService;
import com.todod.service.PublicFileShareService;

@RestController
@RequestMapping("/publicFileShare")
public class PublicFileShareController {

	@Autowired
	private PublicFileShareService publicFileShareService;
	@Autowired
	private IUserRoleService iUserRoleService;
	@Autowired
	private IRoleService iRoleService;
	@Autowired
	private IUserService iUserService;

	/**
	 * 
	 * @param companyId
	 * @param _userId
	 * @param request
	 * @param queryProjectFile
	 * @return
	 */
	@RequestMapping("/getAuthTreeList")
	public ResultData getAuthTreeList(@RequestAttribute Long _userId, Long fileId) {
		User user = iUserService.getById(_userId);
		List<Role> list = iRoleService.getByCompanyId(user.getDepartid());

		List<RoleDto> newList = new ArrayList<RoleDto>();
		List<User> userList = iUserService.getAllList();
		for (Role role : list) {
			List<UserRole> list1 = iUserRoleService.getByRoleIdBesidesUserId(role.getId(), _userId);
			if (list1 != null && list1.size() > 0) {
				// 获取公共数据
				RoleDto role1 = new RoleDto();
				role1.setId(role.getId());
				role1.setParentId(-1l);
				role1.setName(role.getName());
				role1.setDepartid(user.getDepartid());

				newList.add(role1);

				for (UserRole userRole : list1) {
					if (!userRole.getUserId().equals(_userId)) {
						Optional<User> any = userList.stream().filter((x) -> x.getId().equals(userRole.getUserId()))
								.findAny();

						if (any.isPresent()) {
							// 获取公共数据
							RoleDto role2 = new RoleDto();
							role2.setId(any.get().getId());
							role2.setParentId(role.getId());
							role2.setName(any.get().getNickName());
							role2.setDepartid(user.getDepartid());

							newList.add(role2);
						}
					}
				}
			}
		}

		List<PublicFileShare> userRoleList = publicFileShareService.getFileAuthorityByFileIdId(fileId);
		List<Long> hashAuthId = userRoleList.stream().map(x -> x.getUserId()).collect(Collectors.toList());

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("list", newList);
		map.put("hashAuthId", hashAuthId);

		return ResultData.success("ok", map);
	}

	/**
	 * 
	 * @param _userId
	 * @param reqRoleFile
	 * @return
	 */
	@PostMapping("/saveAuthorit")
	public ResultData saveAuthorit(@RequestAttribute Long _userId, @RequestBody ReqRoleFile reqRoleFile) {
		publicFileShareService.saveData(_userId, reqRoleFile.getFileId(), reqRoleFile.getUserIds(),
				reqRoleFile.getType());
		return ResultData.success("ok");
	}

}
